﻿/*
 * ©2009-2010 XELF
 * http://xelf.info
 */

using System;

#if XNA || WINDOWS_PHONE
using Microsoft.Xna.Framework;
#endif

namespace XELF.Framework {
#if XNA || WINDOWS_PHONE
	public static partial class MatrixHelper {
#else
	public partial struct Matrix {
#endif
		/// <summary>
		/// パースペクティブ射影行列をつくる。
		/// </summary>
		/// <param name="fieldOfView"></param>
		/// <param name="aspectRatio"></param>
		/// <param name="nearPlaneDistance"></param>
		/// <param name="farPlaneDistance"></param>
		/// <param name="result"></param>
		public static void CreatePerspectiveFieldOfView(
			float fieldOfView, float aspectRatio, float nearPlaneDistance, float farPlaneDistance,
			out Matrix result) {
			var q = farPlaneDistance / (farPlaneDistance - nearPlaneDistance);
			fieldOfView *= 0.5f;
			result = new Matrix
			{
				M11 = MathHelper.Cot(fieldOfView * aspectRatio),
				M22 = MathHelper.Cot(fieldOfView),
				M33 = -q,
				M34 = -1,
				M43 = -q * nearPlaneDistance,
			};
		}

		/// <summary>
		/// パースペクティブ射影行列をつくる。
		/// </summary>
		/// <param name="fieldOfView"></param>
		/// <param name="aspectRatio"></param>
		/// <param name="nearPlaneDistance"></param>
		/// <param name="farPlaneDistance"></param>
		/// <returns></returns>
		public static Matrix CreatePerspectiveFieldOfView(
			float fieldOfView, float aspectRatio, float nearPlaneDistance, float farPlaneDistance) {
			Matrix result;
			CreatePerspectiveFieldOfView(
				fieldOfView, aspectRatio, nearPlaneDistance, farPlaneDistance, out result);
			return result;
		}
	}

}
